<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8" />
    <title>扫码登录-{eyou:global name='web_name' /}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    <link href="{eyou:global name='web_cmspath'/}/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    {eyou:static file="/template/plugins/wxlogin/skin/css/basic.css" /}
    {eyou:static file="/template/plugins/wxlogin/skin/css/eyoucms.css" /}
    {eyou:include file="wxlogin/skin/css/diy_css.htm" /}

    {eyou:static file="/public/static/common/js/jquery.min.js"/}
    {eyou:static file="/public/plugins/layer-v3.1.0/layer.js"/}
    {eyou:static file="/template/plugins/wxlogin/skin/js/global.js" /}
</head>

<body class="reg regi">
<div class="register_index ey-member">
    <div class="container">
        <form name='mobileForm' id="mobileForm" method="post" class="form-register panel-body fv-form">
            <div class="mip-reg-logo" style="margin-bottom: 30px;"><a href="{eyou:global name='web_cmsurl' /}" class="ey-logo"><img src="{eyou:global name='web_logo'/}" /></a></div>
            <div class="form-group group_z">
                <input type="text" name="mobile" required class="form-control h60" placeholder="手机号码" autocomplete="off" />
            </div>
            <div class="form-group group_z">
                <div class="input-group-icon">
                    <div class="formText">
                        <input type="text" name="mobile_vertify" autocomplete="off" class="form-control h60" placeholder="图片验证码" />
                        <img src="{eyou:url link='api/Ajax/vertify' vars='type=bind' /}" class="chicuele h60" id="mobile_imgVerifys" onclick="mobile_fleshVerify();" title="看不清？点击更换验证码" align="absmiddle"> 
                    </div>
                </div>
            </div>
            <div class="form-group group_z">
                <div class="input-group-icon">
                    <input type="text" name="mobile_code" id="mobile_code" value="" required class="form-control h60" placeholder="短信验证码" autocomplete="off" />
                    <input type="button" id="mobile_code_button" onclick="send_mobile_code();" value="点击获取" class="chicuele btn h60">
                </div>
            </div>
            <input type="hidden" name="referurl" class="form-control" value="{$referurl}"/>
            <input type="hidden" name="wxid" class="form-control" value="{$wxid}"/>
            <input type="hidden" name="__mobile_1_token__" value="{$Request.token.__mobile_1_token__}"/>
            <input  type="button" id="mobile_submit" class="btn btn-lg btn-primary btn-block" value="提交"/>
        </form>
    </div>
</div>

<script type="text/javascript">
    $(document).keydown(function(event){
        if(event.keyCode ==13){
            $('#mobileForm #mobile_submit').trigger("click");
            return false;
        }
    });

    $(function(){
        $("#mobileForm input[name=mobile_vertify]").keyup(function(event){
            var mobile_vertify = $(this).val();
            if (4 <= mobile_vertify.length && 32 <= event.keyCode) {
                send_mobile_code();
            }
        });

        $('#mobileForm #mobile_submit').on('click',function(){
            var mobile = $('#mobileForm input[name=mobile]');
            var mobile_vertify = $('#mobileForm input[name=mobile_vertify]');
            var mobile_code = $('#mobileForm input[name=mobile_code]');
            var wxid = $('#mobileForm input[name=wxid]');

            if($.trim(mobile.val()) == ''){
                layer.msg('手机号码不能为空！', {time: 1500, icon: 2});
                mobile.focus();
                return false;
            }
            // 手机格式不正确
            var reg = /^1[0-9]{10}$/i;
            if (!reg.test($.trim(mobile.val()))) {
                layer.msg('请输入正确的手机号码！', {time: 1500, icon: 5});
                mobile.focus();
                return false;
            }

            if($.trim(mobile_vertify.val()) == ''){
                layer.msg('图形验证码不能为空！', {time: 1500, icon: 5});
                mobile_vertify.focus();
                return false;
            }

            if($.trim(mobile_code.val()) == ''){
                layer.msg('短信验证码不能为空！', {time: 1500, icon: 5});
                mobile_code.focus();
                return false;
            }

            layer_loading();
            $.ajax({
                url: "{eyou:url link='plugins/WxLogin/sp_bind' /}",
                data: $('#mobileForm').serialize(),
                type:'post',
                dataType:'json',
                success:function(res){
                    if (1 == res.code) {
                        if (2 == res.data.status) {
                            layer.closeAll();
                            layer.alert(res.msg, {icon: 6, title: false, closeBtn: false},function(){
                                window.location.href = res.url;
                            });
                        }else{
                            window.location.href = res.url;
                        }
                    } else {
                        layer.closeAll();
                        if ('vertify' == res.data.status) {
                            mobile_fleshVerify();
                        }
                        
                        if (2 == res.data.status) {
                            showErrorAlert(res.msg, 4);
                        } else {
                            if (res.data.fieldname) {
                                $('#mobileForm input[name='+res.data.fieldname+']').focus();
                            }
                            layer.msg(res.msg, {icon: 5,time: 1500});
                        }
                    }
                },
                error : function(e) {
                    layer.closeAll();
                    mobile_fleshVerify();
                    layer.alert(e.responseText, {icon: 5});
                }
            });
            return false;
        });
    });

    function send_mobile_code() {

        var mobile = $.trim($("#mobileForm input[name=mobile]").val());
        // 手机号是否为空
        if (!mobile) {
            $("#mobileForm input[name=mobile]").focus();
            layer.msg('请输入手机号码！', {time: 1500, icon:5});
            return false;
        }
        // 手机格式不正确
        var reg = /^1[0-9]{10}$/i;
        if (!reg.test(mobile)) {
            $("#mobileForm input[name=mobile]").focus();
            layer.msg('请输入正确的手机号码！', {time: 1500, icon:5});
            return false;
        }
        // $('#mobile_vertify_div').show();
        var mobile_vertify = $.trim($("#mobileForm input[name=mobile_vertify]").val());
        if (!mobile_vertify) {
            $("#mobileForm input[name=mobile_vertify]").focus();
            layer.msg('请输入图形验证码！', {time: 1500, icon:5});
            return false;
        }

        // 设置为不可点击
        $("#mobile_code_button").val('获取中…').attr('disabled', 'disabled');
        var __mobile_1_token__ = $('input[name=__mobile_1_token__]').val();

        $.ajax({
            url: __root_dir__+'/index.php?m=api&c=Ajax&a=SendMobileCode&_ajax=1',
            // source:2 登录
            data: {type:'bind', mobile:mobile, title:'第三方绑定账号', source:2,IsVertify:1, vertify:mobile_vertify, __mobile_1_token__:__mobile_1_token__},
            type:'post',
            dataType:'json',
            success:function(res){
                if (res.code == 1) {
                    code_countdown();
                    layer.msg(res.msg, {time: 1500});
                } else {
                    eyou_form_token();
                    $("#mobileForm #mobile_code_button").val('获取验证码').removeAttr("disabled");
                    layer.alert(res.msg, {icon: 2, title: false, closeBtn: 0});
                }
            },
            error : function(e) {
                eyou_form_token();
                $("#mobileForm #mobile_code_button").val('获取验证码').removeAttr("disabled");
                layer.alert(e.responseText, {icon: 5, title: false, closeBtn: 0});
            }
        });
    }

    function mobile_fleshVerify(){
        var src =  __root_dir__+'/index.php?m=api&c=Ajax&a=vertify&type=bind';
        if (src.indexOf('?') > -1) {
            src += '&';
        } else {
            src += '?';
        }
        src += 'r='+ Math.floor(Math.random()*100);
        $('#mobile_imgVerifys').attr('src', src);

        eyou_form_token();
    }

    function code_countdown(){
        // 倒计时
        var setTime;
        var time = 120;
        setTime = setInterval(function() {
            if(0 >= time) {
                clearInterval(setTime);
                return false;
            }

            time--;
            $("#mobileForm #mobile_code_button").val(time + '秒').attr('disabled', 'disabled');
            if(time == 0) $("#mobileForm #mobile_code_button").val('获取验证码').removeAttr("disabled");
        }, 1000);
    }

    function eyou_form_token()
    {
        $.ajax({
            async:false,
            url: __root_dir__+'/index.php?m=api&c=Ajax&a=get_token&name=__mobile_1_token__',
            data: {_ajax:1},
            type:'GET',
            dataType:'html',
            success:function(res1){
                $('input[name=__mobile_1_token__]').val(res1);
            },
            error : function(e) {
                layer.closeAll();
                layer.alert(e.responseText, {icon: 5, title:false});
            }
        });
    }
</script>

</body>
</html>